Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

co-compose

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

co-compose

AdonisJS and Koa style middleware layer with ability to run parallel middleware

  • 5.1.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
17K
decreased by-20.9%
Maintainers
1
Weekly downloads
 
Created
Source

Table of contents

  • Co Compose

Co Compose

Compose an array of functions to be executed one after the other. Similar to Koa and AdonisJS middlewares.

circleci-image typescript-image npm-image license-image

Co compose composes an array of middleware to be executed in sequence. The library is framework independent and can be used in any Javascript project.

Installation

npm i co-compose

# yarn
yarn add co-compose

Usage

Checkout the following example to run an array of middleware functions.

import { Middleware } from 'co-compose'
async function fn1 (next) {
  console.log('executing fn1')
  await next()
}

async function fn2 (next) {
  console.log('executing fn2')
  await next()
}

const middleware = new Middleware()
middleware.register([fn1, fn2])

await middleware.runner().run([])

Passing values

You can also pass values to all middleware functions. An array of values passed to runner.run() will be passed to middleware functions as multiple arguments.

async function fn1 (ctx, next) {
  ctx.stack.push('fn1')
  await next()
}

async function fn2 (ctx, next) {
  ctx.stack.push('fn2')
  await next()
}

const ctx = {
  stack: []
}

await middleware.runner().run([ctx])
assert.deepEqual(ctx.stack, ['fn1', 'fn2'])

Custom resolver

The default behaviour is to define middleware as functions. However, you can define them in any shape and then use a custom resolver to execute them.

Check the following example where ES6 classes are used.

class Middleware1 {
  async handle (ctx, next) {
    ctx.stack.push('fn1')
    await next()
  }
}

class Middleware2 {
  async handle (ctx, next) {
    ctx.stack.push('fn2')
    await next()
  }
}

const middleware = new Middleware()
const ctx = {
  stack: []
}

middleware.register([Middleware1, Middleware2])

await middleware
  .runner()
  .resolve(async function (MiddlewareClass, params) {
    const instance = new MiddlewareClass()
    await instance.handle(...params)
  })
  .run([ctx])

Final Handler

The final handler is a executed when the entire middleware chain ends by calling next. This makes it easier to execute custom functions, which are not part of the chain, however must be executed when chain ends.

The arguments are customizable for the final handler

async function fn1 (ctx, next) {
  ctx.stack.push('fn1')
  await next()
}

async function finalHandler () {
  ctx.stack.push('final handler')
}

const ctx = {
  stack: []
}

await middleware
  .runner()
  .finalHandler(finalHandler, [ctx])
  .run([ctx])

assert.deepEqual(ctx.stack, ['fn1', 'final handler'])

Keywords

FAQs

Package last updated on 12 Apr 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc